package com.example.stars_project_android.adapter;

import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.example.stars_project_android.R;
import com.example.stars_project_android.entity.ConcernStarEnetity;

import java.util.ArrayList;
import java.util.List;

/**
 * 我关注的星适配器
 */
public class ConcernStarListAdapter extends BaseAdapter {
    private Context mContext;
    private int layoutResId;
    private List<ConcernStarEnetity> concernStarList = new ArrayList<>();

    public ConcernStarListAdapter(Context mContext, int layoutResId, List<ConcernStarEnetity> concernStarList) {
        this.mContext = mContext;
        this.layoutResId = layoutResId;
        this.concernStarList = concernStarList;
    }

    @Override
    public int getCount() {
        if(concernStarList!=null) {
            return concernStarList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(concernStarList != null){
            return concernStarList.get(position);
        }
        return 0;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.e("adapter","getView");
        ViewHolder viewHolder;
        if (convertView == null){
            //加载item的布局文件，生成一个View对象，再给converView赋值
            convertView = LayoutInflater.from(mContext).inflate(layoutResId,null);
            //获取每一个item中的控件对象
            viewHolder = new ViewHolder();
            viewHolder.concernStarImg = convertView.findViewById(R.id.iv_concern_star);
            viewHolder.starId = convertView.findViewById(R.id.tv_star_id);
            viewHolder.cancelConcern = convertView.findViewById(R.id.btn_cancel_concern);
            //将ViewHolder对象同converView关联起来
            convertView.setTag(viewHolder);
        }else {//重复利用convertView对象
            //获取到跟当前的convertView绑定在一起的ViewHolder对象
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //给控件对象绑定要显示的数据
        viewHolder.concernStarImg.setImageResource(concernStarList.get(position).getConcernStar());
        viewHolder.starId.setText(concernStarList.get(position).getStarId() + "");
        viewHolder.cancelConcern.setText(concernStarList.get(position).getCancelConcern());
        viewHolder.cancelConcern.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemDeleteListener.onDeleteClick(position);
            }
        });
        return convertView;
    }

    /**
     * 删除按钮的监听接口
     */
    public interface onItemDeleteListener {
        void onDeleteClick(int i);
    }

    private onItemDeleteListener mOnItemDeleteListener;

    public void setOnItemDeleteClickListener(onItemDeleteListener mOnItemDeleteListener) {
        this.mOnItemDeleteListener = mOnItemDeleteListener;
    }

    private static class ViewHolder{
        //每一个item中的控件
        ImageView concernStarImg;
        TextView starId;
        Button cancelConcern;
    }
}
